<?php

declare(strict_types=1);

namespace app\common\service\sys;

use app\common\traits\ServiceTrait;
use app\common\dao\sys\FolderDao;
use think\facade\Db;

/**
 * 我的文件 服务业务层(Service)
 * @author kaige
 * @update  2023-09-01
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class FolderService
{
  protected $dao = null;

  public function __construct()
  {
    $this->dao = new FolderDao();
  }
  use ServiceTrait;


  /**
   * 重写 关联with ，model必须存在with里的方法
   */
  public function _list_with()
  {
    return ['glfoldertype', 'glfiles'];
  }

  /**
   * 重写 列表过滤条件
   */
  public function _list_where($post)
  {
    $where = [];
    $whereOr = [];
    if (isset($post['keywords']) && $post['keywords'] != "") {
      $where[] = ['name', 'like', '%' . $post['keywords'] . '%'];
    }

    if (isset($post['folder_type_id']) && !empty($post['folder_type_id'])) {
      if ($post['folder_type_id'] == 1) {
        $user_id = $post['user_id'];
        $where[] = ['', 'exp', Db::raw("FIND_IN_SET( $user_id , look_user_id)")];
      } else {
        $where[] = ['folder_type_id', '=', $post['folder_type_id']];
      }
    }

    if (isset($post['id']) && !empty($post['id'])) {
      $where[] = ['id', '=', $post['id']];
    }

    if (isset($post['user_id']) && !empty($post['user_id'])) {
      if (isset($post['folder_type_id']) && $post['folder_type_id'] == 1) {
      } else {
        $user_id = $post['user_id'];
        $where[] = ['user_id', '=', $post['user_id']];
        $whereOr[] = ['', 'exp', Db::raw("FIND_IN_SET( $user_id , look_user_id)")];
      }
    }

    $result['whereOr'] = $whereOr;
    $result['where'] = $where;
    $result['field'] = ['*'];
    $result['order'] = ['id' => 'desc'];
    return $result;
  }
}
